<?php
/*
 * @Descripttion: 
 * @version: v1
 * @Author: Terry
 * @Date: 2021-10-05 16:58:49
 * @LastEditors: Terry
 * @LastEditTime: 2024-12-16 12:46:18
 */
declare (strict_types = 1);

namespace app\middleware;
use think\Validator;
use think\exception\ValidateException;
use libs\AES;
class CheckData
{
    /**
     * 处理请求
     *
     * @param \think\Request $request
     * @param \Closure       $next
     * @return Response
     */
    public function handle($request, \Closure $next){
        $data = $this->getData($request);
        $route =   strtolower($request->pathinfo());
        $route  = explode('/',$route);
        $rule = config('rule.'.$route[0].'.'.$route[2].'.'.$route[3]) ?? [];
        if(count($route) > 4)$rule=  config('rule.'.$route[0].'.'.$route[2].'.'.$route[3].'.'.$route[4]) ?? [];
        try { 
            validate($rule)->check($data);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return result(DATA_EMPTY,'',$e->getError());
        }
        $request->data = array_merge($data,$request->authData ?? []);
        return $next($request);
    }
    /**
     * @Descripttion: 获取数据
     * @Author: Terry
     * @Date: 2021-10-06 16:11:46
     * @param {*}
     */    
    private function getData($request){
        //实例化AES
        $method = $request->method();
        $data = $request->$method();
        return $data;
        // $secret = md5($request->prjCode.$request->uuid);
        // $iv  = substr($secret,0,16);
        // $aes = new Aes($secret, 'AES-256-CBC', $iv,  0);  
        // $data = base64_encode($aes->encrypt(json_encode($data)));
        // return  json_decode($aes->decrypt(base64_decode($data)),true);
        // return  json_decode($aes->decrypt(base64_decode($data['data'])),true);
    }
}
